package io.zhifei.modules.sys.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.utils.ExcelHead;
import io.zhifei.common.utils.ExecelUitl;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.modules.sys.system.dao.SysLogisticsDao;
import io.zhifei.modules.sys.system.entity.LogisticsEntity;
import io.zhifei.modules.sys.system.service.SysLogisticsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassNameSysLogisticsImpl
 * @Description
 * @Author
 * @Date2020/11/25 15:39
 * @Version V1.0
 **/
@Service
public class SysLogisticsImpl extends ServiceImpl<SysLogisticsDao, LogisticsEntity> implements SysLogisticsService {

    @Autowired
    private SysLogisticsDao sysLogisticsDao;


    @Override
    public void logisticsImport(MultipartFile file) {
        InputStream in = null;
        try {
            in = file.getInputStream();
            List<ExcelHead> excelHeads = new ArrayList<>();
            //表单解析中文设置
            ExcelHead excelHead = new ExcelHead("单号", "oddNumbers");
            ExcelHead excelHead1 = new ExcelHead("发件人姓名", "hairName");
            ExcelHead excelHead2 = new ExcelHead("发件电话", "hairTel");
            ExcelHead excelHead3 = new ExcelHead("收件人姓名", "takeName");
            ExcelHead excelHead4 = new ExcelHead("收件电话", "takeTel");
            ExcelHead excelHead5 = new ExcelHead("收件省份", "takeProvince");
            ExcelHead excelHead6 = new ExcelHead("收件城市", "takeCity");
            ExcelHead excelHead7 = new ExcelHead("收件区县", "takeArea");
            ExcelHead excelHead8 = new ExcelHead("收件人详细地址", "takeDetailedAddress");
            ExcelHead excelHead9 = new ExcelHead("发件省份", "hairProvince");
            ExcelHead excelHead10 = new ExcelHead("发件城市", "hairCity");
            ExcelHead excelHead11 = new ExcelHead("发件区县", "hairArea");
            ExcelHead excelHead12 = new ExcelHead("发件详细地址", "hairDetailedAddress");
            ExcelHead excelHead13 = new ExcelHead("创建时间", "createTime");
            ExcelHead excelHead14 = new ExcelHead("产品类型", "productType");
            ExcelHead excelHead15 = new ExcelHead("物品类型", "goodsType");
            ExcelHead excelHead16 = new ExcelHead("重量", "weight");
            ExcelHead excelHead17 = new ExcelHead("付款方式", "paymentType");
            ExcelHead excelHead18 = new ExcelHead("运费", "freight");
            ExcelHead excelHead19 = new ExcelHead("业务员", "salesman");
            excelHeads.add(excelHead);
            excelHeads.add(excelHead1);
            excelHeads.add(excelHead2);
            excelHeads.add(excelHead3);
            excelHeads.add(excelHead4);
            excelHeads.add(excelHead5);
            excelHeads.add(excelHead6);
            excelHeads.add(excelHead7);
            excelHeads.add(excelHead8);
            excelHeads.add(excelHead9);
            excelHeads.add(excelHead10);
            excelHeads.add(excelHead11);
            excelHeads.add(excelHead12);
            excelHeads.add(excelHead13);
            excelHeads.add(excelHead14);
            excelHeads.add(excelHead15);
            excelHeads.add(excelHead16);
            excelHeads.add(excelHead17);
            excelHeads.add(excelHead18);
            excelHeads.add(excelHead19);
            List<LogisticsEntity> list = ExecelUitl.readExcelToEntity(LogisticsEntity.class, in, file.getOriginalFilename(), excelHeads);
            for (LogisticsEntity logisticsEntity : list) {
                //查询该订单是否已录入或者是运单号是否为空
                if (!StringUtils.isEmpty(logisticsEntity.getOddNumbers()) && sysLogisticsDao.numberCount(logisticsEntity.getOddNumbers()) == 0) {
                    //新增物流信息
                    sysLogisticsDao.insert(logisticsEntity);
                    //同时修改手机号订单中物流编号
                    sysLogisticsDao.updateOrderNumber(logisticsEntity.getOddNumbers(), logisticsEntity.getTakeTel());
                }
            }
            System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageUtils logisticsList(Integer page, Integer pageSize, String hairName, String taskName, String oddNumber) {
        //构建单号查询
        String[] oddNumbers = new String[0];
        if (!StringUtils.isEmpty(oddNumber)) {
            oddNumbers = oddNumber.split(",");
        }
        Page pageParams = new Page(page, pageSize);
        Page<Map<String, Object>> pageResult = sysLogisticsDao.logisticsList(pageParams, hairName, taskName, Arrays.asList(oddNumbers));
        return new PageUtils(pageResult);
    }

    @Override
    public void delete(String id) {
        LogisticsEntity logisticsEntity = new LogisticsEntity();
        logisticsEntity.setId(Long.valueOf(id));
        logisticsEntity.setFlag(1);
        sysLogisticsDao.updateById(logisticsEntity);
    }
}
